package vn.hanelsoft.newsduet.model.adapter;

import java.util.ArrayList;

import vn.hanelsoft.newsduet.R;
import vn.hanelsoft.newsduet.model.helper.DBHandler;
import vn.hanelsoft.newsduet.model.object.ObjectCategory;
import vn.hanelsoft.newsduet.model.object.ObjectNews;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.aphidmobile.flip.FlipViewController;

public class AdapterFlipview extends BaseAdapter {

	ArrayList<ObjectCategory> array_adapter = new ArrayList<ObjectCategory>();
	FlipViewController controller;
	// Activity activity;
	Context mContext;
	LayoutInflater mInflater;

	// int activeLoadingCount = 0;

	public AdapterFlipview(Context context, ArrayList<ObjectCategory> array) {
		this.mContext = context;
		array_adapter = array;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		int count = 0;
		for (int i = 0; i < array_adapter.size(); i++) {
			int size = array_adapter.get(i).getCategory_listNews().size();
			count += size;
		}
		return count;
	}

	public void replayObjectNews(ObjectNews objectNews) {
		for (int i = 0; i < array_adapter.size(); i++) {
			ObjectCategory objectCategory = new ObjectCategory();
			objectCategory = array_adapter.get(i);
			ArrayList<ObjectNews> listNews = new ArrayList<ObjectNews>();
			for (int j = 0; j < listNews.size(); j++) {
				if (listNews.get(j).getNews_description()
						.equals(objectNews.getNews_description())
						&& listNews.get(j).getNews_link()
								.equals(objectNews.getNews_link())) {
					listNews.remove(j);
					listNews.add(j, objectNews);

					objectCategory.setCategory_listNews(listNews);
					array_adapter.remove(i);
					array_adapter.set(i, objectCategory);

					break;
				}
			}
		}
		notifyDataSetChanged();
	}

	@Override
	public Object getItem(int position) {
		int i = 0;
		int j = position;
		for (; i < array_adapter.size(); i++) {
			int size = array_adapter.get(i).getCategory_listNews().size();
			if (j >= size) {
				j -= size;
			} else {
				break;
			}
		}
		return array_adapter.get(i).getCategory_listNews().get(j);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = new ViewHolder();
		if (convertView == null) {
			// holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_flipview, null);
			holder.webView = (WebView) convertView.findViewById(R.id.webView);
			holder.news_favourite = (ImageView) convertView
					.findViewById(R.id.news_favourite);
			holder.news_favourite_selected = (ImageView) convertView
					.findViewById(R.id.news_favourite_selected);

			// default
			holder.news_favourite.setVisibility(View.VISIBLE);
			holder.news_favourite_selected.setVisibility(View.GONE);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final ObjectNews objectNews = (ObjectNews) getItem(position);
		holder.webView.loadDataWithBaseURL(null,
				objectNews.getNews_description(), "text/html", "utf-8", null);
		if (objectNews.getNews_favourite() != null
				&& objectNews.getNews_favourite().equals("1")) {
			holder.news_favourite
					.setImageResource(R.drawable.favourite_selected);
		} else {
			holder.news_favourite
					.setImageResource(R.drawable.favourite_unselected);
		}
		holder.news_favourite.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.d("Luong", "Luong_______________________ position: "
						+ position);
				String fa = "0";
				if (objectNews.getNews_favourite() != null
						&& objectNews.getNews_favourite().equals("1")) {
					// holder.news_favourite.setVisibility(View.VISIBLE);
					// holder.news_favourite_selected.setVisibility(View.GONE);
					fa = "0";
					DBHandler.deleteNewsByObjectNews(mContext, objectNews);
					Log.d("Luong", "Luong_______________________ set not fa");
				} else {
					// holder.news_favourite.setVisibility(View.GONE);
					// holder.news_favourite_selected.setVisibility(View.VISIBLE);
					fa = "1";
					insertFa(objectNews);
					Log.d("Luong", "Luong_______________________ set fa");
				}
				objectNews.setNews_favourite(fa);
				replayObjectNews(objectNews);

				// //
				// holder.news_favourite.setImageResource(R.drawable.favourite_selected);
				// holder.news_favourite.setVisibility(View.GONE);
				// holder.news_favourite_selected.setVisibility(View.VISIBLE);
				//
				// // save to db fa

			}
		});

		return convertView;
	}

	void insertFa(ObjectNews objectNews) {
		DBHandler.insertNews(mContext, objectNews.getNews_id_category(),
				objectNews.getNews_link(), objectNews.getNews_imageSRC(),
				objectNews.getNews_description(), objectNews.getNews_pubDate(),
				"1");
	}
	
	class ViewHolder {
		WebView webView;
		ImageView news_favourite, news_favourite_selected;
	}

	// void setImageFavourite(String strFavourite)
	// {
	// Log.d("Luong",
	// "+++++++++++++++++++++++++++++++strFavourite: "+strFavourite);
	// if(strFavourite.equals("1"))
	// {
	// this.news_favourite.setImageResource(R.drawable.favourite_selected);
	// }else{
	// this.news_favourite.setImageResource(R.drawable.favourite_unselected);
	// }
	// }

}
